package com.jzmy.site.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.example.commons.entity.TrainingCertificatesDO;
import com.example.commons.utils.ApiResult;
import com.jzmy.site.entity.CertificateProfile;
import com.jzmy.site.entity.vo.OrgRolesListVo;
import com.jzmy.site.service.CertificateProfileService;
import com.jzmy.site.service.OrganizationRolesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author mengzhichao 站点相关接口
 * @create 2022-08-22-13:55
 */
@RestController
@RequestMapping("/organizion")
@Slf4j
public class ApiOrganizionController {

    @Autowired
    private OrganizationRolesService organizationRolesService;

    @Autowired
    private CertificateProfileService certificateProfileService;

    private static String bs = "-";

    /**
     * api小程序端-站点列表数据接口
     *
     * @Description: 站点信息列表查询，参数：id 页码，显示数，站点名称（筛选条件），站点类型（筛选条件）
     * @Param: [id,current, number, name, type,topSearch]
     */
    @GetMapping("/selExaminationPoint")
    @Transactional
    public ApiResult<?> selExaminationPoint(String id, String current, String number, String name, String newType,String topSearch,String newType2,String type) {
        SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss");
        System.out.println("进入查询方法的时间++++++++++++++++++++++++++++++++++++++++++"+sdf2.format(new Date()));
        Map map = new HashMap();
        if (StringUtils.isNotBlank(name)){
            map.put("name",name);
        }
        if (StringUtils.isBlank(topSearch)){ //如果为空不排序
            map.put("topSearch","default");
        }
        if (StringUtils.isNotBlank(topSearch)){
            map.put("topSearch",topSearch);
        }
        if(StringUtils.isNotBlank(newType)){
            map.put("newType",Integer.parseInt(newType));
        }
        if (StringUtils.isNotBlank(id)){
            map.put("id",id);
        }
        if (StringUtils.isNotBlank(newType2)){
            map.put("newType2",newType2);
        }
        Page<OrgRolesListVo> page = organizationRolesService.selectListPage(Integer.parseInt(current), Integer.parseInt(number), map);
        System.out.println("分页数据查询结束的时间++++++++++++++++++++++++++++++++++++++++++"+sdf2.format(new Date()));
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            OrgRolesListVo obj = (OrgRolesListVo) page.getRecords().get(i);
            if (obj.getName().contains("-")){ //如果包含
                String[] array = splitStrArray(obj.getName());
                obj.setName(array[0]);
            }
            if (obj.getProfileId() != null){
                String[] profilesId = obj.getProfileId().split(",");
                if (profilesId.length == 2){ //只展示一个类型
                    String s = profilesId[0];
                    profilesId = new String[1];
                    profilesId[0] = s;
                }
                String returnK = null;
                for(int j = 0;j<profilesId.length;j++){
                    CertificateProfile certificateProfile = certificateProfileService.getById(Long.parseLong(profilesId[j]));
                    if (certificateProfile != null) {
                        if(returnK == null){
                            if ("幼儿照护职业技能等级证书".equals(certificateProfile.getName())){
                                certificateProfile.setName("幼儿照护");
                            }
                            if ("产后恢复职业技能等级证书".equals(certificateProfile.getName())){
                                certificateProfile.setName("产后恢复");
                            }
                            returnK = certificateProfile.getName();
                        }else{
                            if ("幼儿照护职业技能等级证书".equals(certificateProfile.getName())){
                                certificateProfile.setName("幼儿照护");
                            }
                            if ("产后恢复职业技能等级证书".equals(certificateProfile.getName())){
                                certificateProfile.setName("产后恢复");
                            }
                            returnK = returnK + "," + certificateProfile.getName();
                        }
                    }
                }
                obj.setProfileName(returnK);
            }
            if (newType !=null) {
                if (obj.getNewType() != null) {
                    obj.setNewType(newType);
                }
            }
            if (type !=null) {
                if (obj.getNewType() != null) {
                    obj.setNewType(type);
                }
            }
            list.add(obj);
        }
        System.out.println("证书类型转换查询后的时间++++++++++++++++++++++++++++++++++++++++++"+sdf2.format(new Date()));
        page.setRecords(list);
        System.out.println("接口返回时间++++++++++++++++++++++++++++++++++++++++++"+sdf2.format(new Date()));
        return ApiResult.success(page);
    }

    public static String[] splitStrArray(String str) {
        if (str != null) {
            return str.split(bs);
        }
        return null;
    }
}


